//
//  MsgListModel.m
//
//  Created by  殷聃 on 2019/7/4
//  Copyright (c) 2019 yek. All rights reserved.
//

#import "MsgListModel.h"


NSString *const kMsgListModelUserId = @"userId";
NSString *const kMsgListModelDelete = @"delete";
NSString *const kMsgListModelMessageTitle = @"messageTitle";
NSString *const kMsgListModelId = @"id";
NSString *const kMsgListModelMessage = @"message";
NSString *const kMsgListModelAddDate = @"addDate";
NSString *const kMsgListModelRead = @"read";
NSString *const kMsgListModelMessageType = @"messageType";


@interface MsgListModel ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation MsgListModel

@synthesize userId = _userId;
@synthesize delete = _delete;
@synthesize messageTitle = _messageTitle;
@synthesize internalBaseClassIdentifier = _internalBaseClassIdentifier;
@synthesize message = _message;
@synthesize addDate = _addDate;
@synthesize read = _read;
@synthesize messageType = _messageType;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.userId = [self objectOrNilForKey:kMsgListModelUserId fromDictionary:dict];
            self.delete = [[self objectOrNilForKey:kMsgListModelDelete fromDictionary:dict] boolValue];
            self.messageTitle = [self objectOrNilForKey:kMsgListModelMessageTitle fromDictionary:dict];
            self.internalBaseClassIdentifier = [[self objectOrNilForKey:kMsgListModelId fromDictionary:dict] doubleValue];
            self.message = [self objectOrNilForKey:kMsgListModelMessage fromDictionary:dict];
            self.addDate = [[self objectOrNilForKey:kMsgListModelAddDate fromDictionary:dict] doubleValue];
            self.read = [[self objectOrNilForKey:kMsgListModelRead fromDictionary:dict] boolValue];
            self.messageType = [self objectOrNilForKey:kMsgListModelMessageType fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.userId forKey:kMsgListModelUserId];
    [mutableDict setValue:[NSNumber numberWithBool:self.delete] forKey:kMsgListModelDelete];
    [mutableDict setValue:self.messageTitle forKey:kMsgListModelMessageTitle];
    [mutableDict setValue:[NSNumber numberWithDouble:self.internalBaseClassIdentifier] forKey:kMsgListModelId];
    [mutableDict setValue:self.message forKey:kMsgListModelMessage];
    [mutableDict setValue:[NSNumber numberWithDouble:self.addDate] forKey:kMsgListModelAddDate];
    [mutableDict setValue:[NSNumber numberWithBool:self.read] forKey:kMsgListModelRead];
    [mutableDict setValue:self.messageType forKey:kMsgListModelMessageType];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.userId = [aDecoder decodeObjectForKey:kMsgListModelUserId];
    self.delete = [aDecoder decodeBoolForKey:kMsgListModelDelete];
    self.messageTitle = [aDecoder decodeObjectForKey:kMsgListModelMessageTitle];
    self.internalBaseClassIdentifier = [aDecoder decodeDoubleForKey:kMsgListModelId];
    self.message = [aDecoder decodeObjectForKey:kMsgListModelMessage];
    self.addDate = [aDecoder decodeDoubleForKey:kMsgListModelAddDate];
    self.read = [aDecoder decodeBoolForKey:kMsgListModelRead];
    self.messageType = [aDecoder decodeObjectForKey:kMsgListModelMessageType];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_userId forKey:kMsgListModelUserId];
    [aCoder encodeBool:_delete forKey:kMsgListModelDelete];
    [aCoder encodeObject:_messageTitle forKey:kMsgListModelMessageTitle];
    [aCoder encodeDouble:_internalBaseClassIdentifier forKey:kMsgListModelId];
    [aCoder encodeObject:_message forKey:kMsgListModelMessage];
    [aCoder encodeDouble:_addDate forKey:kMsgListModelAddDate];
    [aCoder encodeBool:_read forKey:kMsgListModelRead];
    [aCoder encodeObject:_messageType forKey:kMsgListModelMessageType];
}

- (id)copyWithZone:(NSZone *)zone
{
    MsgListModel *copy = [[MsgListModel alloc] init];
    
    if (copy) {

        copy.userId = [self.userId copyWithZone:zone];
        copy.delete = self.delete;
        copy.messageTitle = [self.messageTitle copyWithZone:zone];
        copy.internalBaseClassIdentifier = self.internalBaseClassIdentifier;
        copy.message = [self.message copyWithZone:zone];
        copy.addDate = self.addDate;
        copy.read = self.read;
        copy.messageType = [self.messageType copyWithZone:zone];
    }
    
    return copy;
}


@end
